package net.sdd.ucc.service.impl;
import java.util.Date;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import net.sdd.ucc.dto.LoginDto;
import net.sdd.ucc.dto.RegisterDto;
import net.sdd.ucc.entity.Member;
import net.sdd.ucc.exception.Asserts;
import net.sdd.ucc.exception.BusinessException;
import net.sdd.ucc.mapper.MemberMapper;
import net.sdd.ucc.service.IMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sdd.ucc.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Jet
 * @since 2022-08-26
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements IMemberService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void register(RegisterDto registerDto) {
        //1. 参数校验
        if (StringUtils.isEmpty(registerDto.getNickname())
                || StringUtils.isEmpty(registerDto.getPassword())
                || StringUtils.isEmpty(registerDto.getMobile())
                || StringUtils.isEmpty(registerDto.getCode())) {
            Asserts.fail(50010,"参数为空，注册失败！");
        }

        //2. 校验验证码
        //2.1 先获取Redis中的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(registerDto.getMobile());
        //2.2 验证码校验
        if (StringUtils.isEmpty(redisCode) || !redisCode.equals(registerDto.getCode())) {
            Asserts.fail(50020,"验证码错误！");
        }

        //3、校验手机号是否重复
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",registerDto.getMobile());
        // wrapper.select("id"); 指定查询返回的列
        Long count = baseMapper.selectCount(wrapper);
        if (count>0) {
            Asserts.fail(50030,"手机号码已被注册，换个手机号再试！");
        }

        //4、通过hutool工具包，对密码加密
        String hashpw = BCrypt.hashpw(registerDto.getPassword());

        //5、保存注册数据
        Member member = new Member();
        member.setMobile(registerDto.getMobile());
        member.setPassword(hashpw);
        member.setNickname(registerDto.getNickname());
        member.setAvatar("https://my-bucket-test8080.oss-cn-shenzhen.aliyuncs.com/image/1.jpeg");
        member.setIsDisabled(false);
        baseMapper.insert(member);
    }

    @Override
    public String login(LoginDto loginDto) {
        String mobile = loginDto.getMobile();
        String password = loginDto.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            Asserts.fail(5000,"手机号或密码错误");
        }

        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Member member = baseMapper.selectOne(queryWrapper);
        if (member == null) {
            Asserts.fail(5000,"手机号或密码错误");
        }

        if (!BCrypt.checkpw(password,member.getPassword())) {
            Asserts.fail(5000,"手机号或密码错误");
        }

        String token = JwtUtils.getJwtToken(member.getId(), member.getNickname());
        return token;

    }


    public static void main(String[] args) {
        // 密码加密
        //String hashpw = BCrypt.hashpw("123456");
        //System.out.println("加密后的密码 = " + hashpw);

        // 校验
        //boolean flag = BCrypt.checkpw("123 456", hashpw);
        //System.out.println("flag = " + flag);

        // 为什么每次密码都不一样？
        // 随机盐
        String gensalt = BCrypt.gensalt();
        System.out.println("随机盐：" + gensalt);
        // 加密
        String hashpw = BCrypt.hashpw("123456",gensalt);
        System.out.println("加密后的密码 = " + hashpw);

    }
}
